package com.qf.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.qf.unionprogram.dao.UserDao;
import com.qf.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

//import com.qf.springsecurity.dao.UserDao;

@Service("UserDetail")
public class UserDetail implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String phone_number) throws UsernameNotFoundException {

        System.out.println(phone_number);
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("phone_number",phone_number);
        com.qf.demo.entity.User user = userDao.selectOne(wrapper);

        String name=user.getPhone_number();
        String password=user.getPassword();
        String auth=user.getAuth();


        //设置角色，角色的概念在之后章节介绍
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList(auth);
        //可以从数据库获取用户名和密码
        return new User(name,new BCryptPasswordEncoder().encode(password),auths);
    }



}
